package com.zookeeper.t4;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.List;

/**
 * 作者: LCG
 * 日期: 2020/7/14 11:43
 * 描述:
 */
public class TestZookeeper4 {

    public static void main(String[] args) throws IOException, KeeperException, InterruptedException {

        //session的超时间是 毫秒单位
        ZooKeeper zooKeeper=new ZooKeeper("192.168.137.1:2181",30000,new MyWatcher001());

        //创建一个节点
        zooKeeper.create("/TTT","test create".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);


        //获取数据
        byte[] data = zooKeeper.getData("/TTT", true, null);

        System.out.println(new String(data));

        //设置数据
        Stat stat = zooKeeper.setData("/TTT", "测试数据".getBytes(), -1);

        System.out.println(stat.getVersion());

        //获取子节点
        List<String> children = zooKeeper.getChildren("/TTT", true);
        zooKeeper.create("/TTT/t1","子节点数据".getBytes(),ZooDefs.Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);

        //删除一个节点
        byte[] data1 = zooKeeper.getData("/TTT/t1", true, null);
        zooKeeper.delete("/TTT/t1",-1);


        zooKeeper.close();

    }

}
